package controllers;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import models.Attachment;
import models.Audio;


import org.apache.commons.io.FileUtils;
import play.Play;
import play.mvc.Controller;
import play.mvc.With;
import service.AudioService;

import util.ResourceOperate;

@With(Secure.class)
public class AudioAction extends Controller {

	public static String[] types = { "mp3", "wma", "flac", "aac", "mmf", "amr",
			"m4a", "m4r.ogg", "mp2", "wav", "wavpack" };

	/**
	 * 全查
	 */
	@Check("ResourceManage")
	public static void list() {
		List<Audio> audList = AudioService.findAll();
		render(audList);
	}

	@Check("ResourceManage")
	public static void blank() {
		render();
	}

	/**
	 * 添加
	 * 
	 * @param name
	 * @param file
	 * @param dis
	 */
	@Check("ResourceManage")
	public static void createAud(File file, String audDis) {
		try{
			boolean b = (Boolean) ResourceOperate.upload(file,types).get(0);
			if(b){
				String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
				Audio aud = new Audio();
				aud.audName = ResourceOperate.splitStr(file.getName()).get(0).toString(); // 拿到文件名
				aud.audPath = uploadPath;
				aud.audDis = audDis;
				AudioService.crate(aud);
				flash.success("上传成功 ！");
				list();
			}else{
				flash.success("上传失败 ！");
				list();
			}
		} catch (Exception e) {
			blank();
			flash.success("上传失败 ！");
			e.printStackTrace();
			list();
		}
	}

	/**
	 * 修改信息
	 * 
	 * @param id
	 */
	@Check("ResourceManage")
	public static void show(long id) {
		Audio aud = AudioService.findById(id);
		render(aud);
	}

	/**
	 * 修改
	 * 
	 * @param name
	 * @param path
	 * @param dis
	 * @param file
	 * @param id
	 */
	@Check("ResourceManage")
	public static void saveAud(String audName, String audPath, String audDis,
			File file, long id) {
		try {

			Audio aud = AudioService.findById(id);
			String oldName = aud.audName;
			aud.audName = audName;
			aud.audDis = audDis;
			if (file != null) {
				boolean bb = (Boolean) ResourceOperate.upload(file,types).get(0);
				if(bb){
					boolean b = ResourceOperate.deleteFile(Play.applicationPath + audPath);
					if (b) {
						String uploadPath = ResourceOperate.upload(file, types).get(1).toString();
						String ends =  ResourceOperate.splitStr(uploadPath).get(1).toString();
						aud.audPath = uploadPath.replace(file.getName().toString(), audName+"."+ends); // 原名
						ResourceOperate.reName(uploadPath, audName); 		// 修改文件夹中的文件名称
					}
					AudioService.save(aud); 	// 将修改后的数据保存到数据库中
					flash.success("修改成功 ！");
					list();
				}else{
					flash.success("修改失败 ！");
					list();
				}	
			} else {
				String path = audPath.replace(oldName, audName); // 将目录下的文件名称修改
				aud.audPath = path;
				ResourceOperate.reName(audPath, audName);
				AudioService.save(aud); 	// 将修改后的数据保存到数据库中
				flash.success("修改成功 ！");
				list();
			}
				
		} catch (Exception e) {
			flash.success("修改失败 ！");
			list();
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 */
	@Check("ResourceManage")
	public static void deleteAud(Long id) {
		String audPath = Play.applicationPath
				+ AudioService.findById(id).audPath;
		boolean b = ResourceOperate.deleteFile(audPath);
		if (b) {
			AudioService.delete(id);
			flash.success("删除成功 ！");
		} else {
			flash.success("删除失败 ！");
		}
		list();
	}

}
